//
//  Login_BtConnetViewController.m
//  TY-C
//
//  Created by xyyl on 2024/2/29.
//

#import "Login_BtConnetViewController.h"
#import "Login_BtConnectTableViewCell.h"
#import "CommandViewController.h"
#import "TY_DataCommand.h"

@interface Login_BtConnetViewController ()<UITableViewDelegate,UITableViewDataSource>
///
@property (nonatomic,strong) UIButton * searchBtn;

///
@property (nonatomic,strong) UITableView * tableView;

///点击搜索后的背景页
@property (nonatomic,strong) UIView * whiteView;


@end

@implementation Login_BtConnetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
/// 蓝牙连接页面。
    self.view.backgroundColor = RGBColor(241, 249, 253);
    
    [self initTabelView];
    [self initSearchBtn];

    NSLog(@"%@",NSStringFromCGRect(self.view.bounds));
}


- (void)initSearchBtn {
    _searchBtn = [UIButton new];
    [self.view addSubview:_searchBtn];
    
    [_searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(30*2));
        make.width.equalTo(@(80 * 2));
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20*2);
    }];
    [_searchBtn setTitle:@"搜索设备" forState:0];
    [_searchBtn setBackgroundColor:RGBColor(38, 167, 222)];
    _searchBtn.layer.cornerRadius = 30;
    _searchBtn.titleLabel.font = ZYQFONT(24);
    [_searchBtn addTarget:self action:@selector(searbtnAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)searbtnAction {
    NSLog(@"搜索shebei");
//    [TY_DataCommand updateData];
//    [TY_DataCommand checkAllData];
    [self initWhiteView];

}

- (void)cellConBtnAction:(UIButton *)btn {
    NSInteger tag = btn.tag;
    
    ///点击cell连接后
//    [TY_DataCommand delData];
//    [TY_DataCommand addData];
    
//    return;
    [self initWhiteView];
    
    
}

- (void)initTabelView {
    
//    CGFloat sssW = 160.0/240.0;
    CGFloat sssX = SSSw * ZYQWIDTH;
//    CGFloat sssH = 80.0/400;
    CGFloat sssY = ZYQHEIGHT * SSSh;
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(sssX, sssY, ZYQWIDTH - sssX * 2, ZYQHEIGHT - sssY * 2) style:0];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle = 0;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.scrollIndicatorInsets = _tableView.contentInset;
    }
    [self.view addSubview:_tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Login_BtConnectTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"id"];
    if (cell == nil) {
        cell = [[Login_BtConnectTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"];
        cell.selectionStyle = 0;
        cell.backgroundColor = [UIColor whiteColor];
    }
    cell.connectBtn.tag = indexPath.row;
    if (indexPath.row == 1) {
        cell.connectBtn.selected = YES;
    }else{
        cell.connectBtn.selected = NO;

    }
    [cell.connectBtn addTarget:self action:@selector(cellConBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}



- (void)initWhiteView {
    _tableView.hidden = YES;
    _searchBtn.hidden = YES;
    if(_whiteView == nil){
        
        _whiteView = [UIView new];
        [self.view addSubview:_whiteView];
        _whiteView.frame = self.view.bounds;
        _whiteView.backgroundColor = [UIColor whiteColor];
        
        
        
        UIButton * leftBtn = [UIButton new];
        [_whiteView addSubview:leftBtn];
        
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_whiteView).offset(50*IPADSCALE);
            make.top.equalTo(_whiteView).offset(45*IPADSCALE);
            make.bottom.equalTo(_whiteView.mas_bottom).offset(-45*IPADSCALE);
            make.width.equalTo(@(190*IPADSCALE));
        }];
        [leftBtn setBackgroundColor:RGBColor(242, 251, 246)];
        [self leftBtnAddViewS:leftBtn WithIconName:@"bl_pg" WithTitleName:@"评估"];
    
        
        
        
        UIButton * topBtn = [UIButton new];
        
        [_whiteView addSubview:topBtn];
        
        [topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(leftBtn.mas_right).offset(30*IPADSCALE);
            make.top.equalTo(leftBtn);
            make.right.equalTo(_whiteView.mas_right).offset(-50*IPADSCALE);
            make.height.equalTo(@(150*IPADSCALE));
        }];
        [topBtn setBackgroundColor:RGBColor(241, 249, 253)];
        [self leftBtnAddViewS:topBtn WithIconName:@"bl_pg1" WithTitleName:@"处方管理"];

        
        UIButton * bBtnL = [UIButton new];
        
        [_whiteView addSubview:bBtnL];
        
        [bBtnL mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(topBtn.mas_bottom).offset(25*IPADSCALE);
            make.left.equalTo(topBtn);
            make.bottom.equalTo(leftBtn.mas_bottom);
            make.right.equalTo(topBtn.mas_centerX).offset(-15*2);
            make.height.equalTo(bBtnL.mas_width);
        }];
        [bBtnL setBackgroundColor:RGBColor(252, 245, 237)];
        [self leftBtnAddViewS:bBtnL WithIconName:@"bl_pg2" WithTitleName:@"多媒体生物反馈"];

        
        
        UIButton * bBtnR = [UIButton new];
        
        [_whiteView addSubview:bBtnR];
        
        [bBtnR mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.width.equalTo(bBtnL);
            make.left.equalTo(topBtn.mas_centerX).offset(15*2);
        }];
        [bBtnR setBackgroundColor:RGBColor(242, 251, 246)];
        [self leftBtnAddViewS:bBtnR WithIconName:@"bl_pg3" WithTitleName:@"详情"];
        
        leftBtn.tag = 0;
        topBtn.tag = 1;
        bBtnL.tag = 2;
        bBtnR.tag = 3;
        [leftBtn addTarget:self action:@selector(topBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [topBtn addTarget:self action:@selector(topBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [bBtnL addTarget:self action:@selector(topBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [bBtnR addTarget:self action:@selector(topBtnAction:) forControlEvents:UIControlEventTouchUpInside];


    }
    
}

- (void)topBtnAction:(UIButton *)btn {
    CommandViewController * cvc = [[CommandViewController alloc]init];
    [self.navigationController pushViewController:cvc animated:YES];
}

- (void)leftBtnAddViewS:(UIButton *)btn WithIconName:(NSString *)imgName WithTitleName:(NSString * )titleName{
    
    UIImageView * imgV = [UIImageView new];
    UILabel * llL = [UILabel new];
    [btn addSubview:imgV];
    [btn addSubview:llL];

    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.equalTo(@60);
        make.center.equalTo(btn);
    }];
    [imgV setImage:[UIImage imageNamed:imgName]];
    
    [llL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imgV.mas_bottom).offset(10*IPADSCALE);
        make.height.equalTo(@(22.5*2));
        make.left.width.equalTo(btn);
    }];
    llL.textColor = [UIColor blackColor];
    llL.font = ZYQFONT(28);
    llL.textAlignment = NSTextAlignmentCenter;
    llL.text = titleName;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    CGFloat yscale = [@"" oppositeY:27 ToBigY:240];

    return (27+10)*IPADSCALE;
}




@end
